import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.File;
import java.net.URL;

/**
 * Created by fanshaohua on 2017/12/30.
 */
public class TestTestng {


    /**
     * Created by fanshaohua on 2017/8/5.
     */
    public class appiumTest {
        public AppiumDriver driver;
        private boolean isInstall = false;

        @BeforeTest
        public void setUp() throws Exception{
            //启动appium
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setCapability("deviceName","3aa83130");
            cap.setCapability("automationName","Appium");
            cap.setCapability("platformName","Android");
            cap.setCapability("platformVersion","6.0.1");


            //配置测试apk
            cap.setCapability("appPackage","com.shixinyun.zuobiao");
            cap.setCapability("appActivity","com.shixinyun.zuobiao.ui.SplashActivity");
            cap.setCapability("sessionOverride",true);  //每次启动时覆盖session，否则第二次运行会报错不能新建session
            cap.setCapability("unicodeKeyboard",true);  //设置键盘
            cap.setCapability("resetKeyboard",false);   //设置默认键盘为appium的键盘
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);

            //如果真机设备已经安装坐标则不需要重新安装
            if (isInstall){
                File classpathRoot = new File(System.getProperty("user.dir"));
                File appDir = new File(classpathRoot, "apps");
                File app = new File(appDir,"zuobiao_v2.0.0-beta2_debug_def.apk");
                cap.setCapability("app",app.getAbsolutePath());
            }
        }

        @AfterTest
        public void tearDown() throws Exception {
           driver.quit();
        }

        @Test
        public void testZuobiao() throws Exception{

            Thread.sleep(5000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/skip_btn")).click();
            System.out.println("点击立即体验");
            Thread.sleep(2000);
            driver.findElement(By.id("android:id/button1")).click();
            System.out.println("点击允许");
            Thread.sleep(1000);
            driver.findElement(By.id("android:id/button1")).click();
            System.out.println("点击允许");
            Thread.sleep(1000);
            driver.findElement(By.id("android:id/button1")).click();
            System.out.println("点击允许");
//            Thread.sleep(1000);
//            driver.findElement(By.id("android:id/button1")).click();
//            System.out.println("点击允许");
            Thread.sleep(1000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/account_et")).sendKeys("664170687@qq.com");
            System.out.println("输入帐号");
            Thread.sleep(1000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/password_et")).sendKeys("123456");
            System.out.println("输入密码");
            Thread.sleep(1000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/login_btn")).click();
            System.out.println("点击登录");
            Thread.sleep(3000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/guide_close_tv")).click();
            System.out.println("点击关闭浮层");
            Thread.sleep(20000);
            DoScreen a = new DoScreen();
            a.swipeToUp(driver,1000,3);
            a.swipeToDown(driver,1000,3);
            System.out.println("划屏：上划3次，下划3次");
            Thread.sleep(3000);
            driver.findElement(By.name("测试测试")).click();
            System.out.println("进入测试测试的群");
            Thread.sleep(3000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/chat_message_et")).sendKeys("***appium+java+selenium+testNG自动化测试已实现***,now testing success");
            System.out.println("在输入框输入文字");
            Thread.sleep(3000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/chat_send_btn")).click();
            System.out.println("点击发送");
            Thread.sleep(1000);
            driver.findElement(By.id("com.shixinyun.zuobiao:id/back")).click();
            System.out.println("点击返回");

        }
    }
}
